package com.yazdankhah.beans;

import com.yazdankhah.application.Const;
import com.yazdankhah.listeners.ContextListener;
import com.yazdankhah.utils.FileUtil;
import com.yazdankhah.utils.GenUtil;
import com.yazdankhah.utils.StringUtil;
import com.yazdankhah.utils.UnicodeUtil;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.text.BreakIterator;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;

/**
 *
 * @author ahmad
 */
public class SubTrans {
  
  private String transCombinationStr;
  private List<String> subTranslationsList;
  private int subTransIndex = 0;
  
  //private static Logger logger = Logger.getLogger(SubTrans.class);
  private static Logger logger = null;
  
  static {
    String log4jFile = "C:\\MyApps\\qtt\\src\\main\\webapp" + Const.LOG_HOME + "log4j.xml";
    DOMConfigurator.configureAndWatch(log4jFile, 300000);
    logger = Logger.getLogger(SubTrans.class);
    logger.info("qtt Temp logging started; Log4J configured using: " + log4jFile);
  }


  public SubTrans(String transCombinationStr) {
    
    System.out.println("transCombinationStr in Constructor = [" + transCombinationStr + "]");
    this.subTranslationsList = new ArrayList<>();
    this.transCombinationStr = transCombinationStr;
  }
  
  
  public String nextCombinedTranslations() {
    
    subTransIndex++;
    System.out.println("subTransIndex = [" + subTransIndex + "]");
    
    if (subTransIndex < subTranslationsList.size()) {
      return subTranslationsList.get(subTransIndex);
    } else {
      return subTranslationsList.get(--subTransIndex);
    }
    
  } //method
  
  
  public String previousCombinedTranslations() {
    
    subTransIndex--;
    
    if (subTransIndex >= 0) {
      return subTranslationsList.get(subTransIndex);
    } else {
      return subTranslationsList.get(++subTransIndex);
    }
    
  } //method
  
  
  public String breakTranslations() {
    
    String[][] translationsTable = constructTableOfSubTranslations(transCombinationStr);
    GenUtil.showStringArray(translationsTable, 5, "translationsTable in breakTranslations");
    
    subTranslationsList = convertTableColumns2StrArr(translationsTable);
    GenUtil.showArrayList(subTranslationsList);
    
    System.out.println("subTranslationsList.size() = [" + subTranslationsList.size() + "]");
    
    return subTranslationsList.get(subTransIndex);
    
  } //method
  

  private static String[][] constructTableOfSubTranslations(String combination) {
    
    String[] translations = combination.split("[\n]");
    
    Locale locale = new Locale("fa");
    
    
    //System.out.println("translations in constructTableOfSubTranslations = [" + translations + "]");
    //GenUtil.showStringArray(translations, "constructTableOfSubTranslations");
    
    String[][] translationsTable = new String[translations.length][];
    
    int index = 0;
    
    for (String trans : translations) {
      
      if (trans.isEmpty()) continue;
      
      System.out.println("trans = [" + trans + "]");
      
      //String[] splittedTrans = trans.split("[~]");
      String[] splittedTrans = simpleTokenizer(trans, locale);
      
      GenUtil.showStringArray(splittedTrans, "splittedTrans");
      
      translationsTable[index++] = splittedTrans;
      
    } //for
    
    GenUtil.showStringArray(translationsTable, 20, "translationsTable in constructTableOfSubTranslations");
    
    return translationsTable;
    
  } //method
  
  private static void testconstructTableOfSubTranslations() {
    
    String combination = "s11~s12~s13\ns21~s22~s23\n";
    
    String[][] translationsTable = constructTableOfSubTranslations(combination);
    
    //GenUtil.showStringArray(translationsTable, 5, "translationsTable in constructTableOfSubTranslations");
    
  } //method
  
  
  private static List<String> convertTableColumns2StrArr(String[][] translationsTable) {
    
    List<String> subTranses = new ArrayList<>();
    
    StringBuilder sb;
    
    for (int j = 0; j < translationsTable[0].length; j++) {
      sb = new StringBuilder();
      
      for (int i = 0; i < translationsTable.length; i++) {
        sb.append(translationsTable[i][j]).append("\n");
      }
      
      subTranses.add(sb.toString());
      
    } //for
    
    return subTranses;
    
  } //method
  
  private static void testconvertTableColumns2StrArr() {
    
    String combination = "s11~s12~s13\ns21~s22~s23\n";
    
    String[][] translationsTable = constructTableOfSubTranslations(combination);
    //GenUtil.showStringArray(translationsTable, 5);
    
    List<String> arr = convertTableColumns2StrArr(translationsTable);
    GenUtil.showArrayList(arr);
    
  } //method
  
  
  /**
     * Tokenize the given string and return an array list containing the tokens.
     * post-condition: the tokens cases are INTACT.
     *
     * @param inpStr - the given input string
     * @param locale - the locale of the language
     * @return the tokens in an array list
     */
    public static String[] simpleTokenizer(String inpStr, Locale locale) {
      
        //int avgSize = inpStr.length() / AVG_WORD_SIZE;
        List<String> tokens = new ArrayList();

        BreakIterator iter = BreakIterator.getSentenceInstance(locale);
        iter.setText(inpStr);
        
        String tkn = "";

        int start = iter.first();
        int end   = iter.next();
        
        System.out.println("start = " + start);
        System.out.println("end = " + end);
        

        while (end != BreakIterator.DONE) {

            tkn = inpStr.substring(start, end).trim();

            if (tkn.isEmpty()) continue;
                
            tokens.add(tkn);

            start = end;
            end = iter.next();
            System.out.println("start = " + start);
            System.out.println("end = " + end);
        } //while
        
       return tokens.toArray(new String[tokens.size()]);

    } //method


    private static void testsimpleTokenizer()
    {
        Locale locale = new Locale("fa");
        String inFN  = "quran\\testUTF8.txt";
        String outFN  = "quran\\testUTF8_Out.txt";
        
        
        String docStr = FileUtil.fileToString(inFN);
        System.out.println("docStr = [" + docStr + "]");
        
        docStr = docStr.replaceAll("~", ". ");
        
//        List<String> tokens = simpleTokenizer(docStr, locale);
//        
//        //GenUtil.showArrayList(tokens);
//        
//        FileUtil.arrayListToFile(tokens, outFN);

    } //test method
    
    
    private static void myTests() {
      
      //int avgSize = inpStr.length() / AVG_WORD_SIZE;
      //List<String> tokens = new ArrayList();
        
      //Locale locale = new Locale("fa");
      String inFN  = "quran\\t.txt";
      String outFN  = "quran\\t2_Out.txt";

      String docStr = FileUtil.fileToString(inFN);
      UnicodeUtil.showUnicodeStringInfo(docStr, logger);
      UnicodeUtil.writeUnicodeStringInfo(docStr, outFN);
      
//      docStr = docStr.replaceAll("~", ". ");
//      String outFN2  = "quran\\t2_Out.txt";
//      UnicodeUtil.writeUnicodeStringInfo(docStr, outFN2);
      
      String[] splittedDoc = docStr.split("[~]");
      //String outFN3  = "quran\\t3_Out.txt";
      UnicodeUtil.showUnicodeStringInfo(splittedDoc[0], logger);
      
      //String outFN4  = "quran\\t4_Out.txt";
      UnicodeUtil.showUnicodeStringInfo(splittedDoc[1], logger);
      
      

      //System.out.println("docStr = [" + docStr + "]");
      //docStr = docStr.replaceAll("~", ". ");

    } //method


// ........................ M A I N   M E T H O D ............................//
  /**
   * This main method is just for testing this class.
   *
   * @param args - the arguments
   */
  public static void main(String[] args) throws UnsupportedEncodingException {
    
//    String unicodeMessage = "\u7686\u3055\u3093\u3001\u3053\u3093\u306b\u3061\u306f";
//    PrintStream out = new PrintStream(System.out, true, "UTF-8");
//    out.println(unicodeMessage);
    
    /* ....................................................*/
    myTests();
    /* ....................................................*/

  } //main method
  
} //class
